package com.martinwj.service.impl;

import com.martinwj.constant.ErrorMsg;
import com.martinwj.dao.api.IApiDAO;
import com.martinwj.dao.media.IMediaDAO;
import com.martinwj.dao.tag.ITagDAO;
import com.martinwj.dao.video.IVideoDAO;
import com.martinwj.entity.Api;
import com.martinwj.entity.Field;
import com.martinwj.entity.Video;
import com.martinwj.exception.SysException;
import com.martinwj.service.ApiService;
import it.sauronsoftware.jave.VideoInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: ApiServiceImpl
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2020-12-22
 */
@Service
public class ApiServiceImpl implements ApiService {

    @Autowired
    private IApiDAO iApiDAO;
    @Autowired
    private ITagDAO iTagDAO;


    /**
     * 查询指定类型的接口列表
     * @param type 接口类型
     * @return
     */
    public List<Api> listByType(String type) {
        return iApiDAO.listByType(type);
    }

    /**
     * 保存接口
     * @param slideList
     */
    public void save(List<Api> slideList) {
        for (Api slide : slideList) {
            if (StringUtils.isEmpty(slide.getId())) {
                // 插入
                iApiDAO.insert(slide);
            } else {
                // 更新
                iApiDAO.update(slide);
            }
        }
    }

    /**
     * 删除接口
     * @param idArr 接口主键数组
     */
    public void delete(String[] idArr) {
        // 批量删除接口
        iApiDAO.delete(idArr);
    }

    /**
     * 根据主键，获取接口设置条件
     * @param id 主键
     * @return
     */
    public Map<String, Object> selectById(String id) {
        return iApiDAO.selectById(id);
    }

    /**
     * 保存自定义数据条件设置
     * @param radioList
     * @param param
     */
    public void userSave(List<Field> radioList, Map<String, Object> param) {
        // 1.0 处理标签
        if (StringUtils.isEmpty(param.get("tag"))) {
            // 清空该媒体的标签
            param.put("tag", "");
        } else {
            String tags = param.get("tag").toString();
            // 将得到的标签转为数组格式
            String[] tagArr = tags.split(",");
            // 根据用户输入的标签，查询标签的主键，按从小到大排序
            List<String> idList = iTagDAO.listIdByNameArr(tagArr);

            // 将集合转为 1,3这种字符串
            String str = "";
            for (int i=0; i<idList.size(); i++) {
                if (i==0) {
                    str = idList.get(i);
                } else {
                    str = str + "," + idList.get(i);
                }
            }

            // 保存新的标签
            param.put("tag", str);
        }

        // 2.0 生成更新数据SQL文
        StringBuffer sql = new StringBuffer();
        sql.append(" UPDATE ");
        sql.append(" api ");
        sql.append(" SET ");
        for (Field field : radioList) {
            String value = "";
            if (param.get(field.getVarName())!=null) {
                value = param.get(field.getVarName()).toString();
            }
            sql.append(field.getVarName() + "=" + "'" + value + "',");
        }
        sql.append(" typeId='"+param.get("typeId").toString()+"', ");
        sql.append(" num='"+param.get("num").toString()+"', ");
        sql.append(" tag='"+param.get("tag").toString()+"', ");
        sql.append(" selectVideo='"+param.get("selectVideo").toString()+"', ");
        sql.append(" cacheTime='"+param.get("cacheTime").toString()+"', ");
        sql.append(" field='"+param.get("field").toString()+"' ");
        sql.append(" WHERE ");
        sql.append(" id = '" + param.get("id").toString() + "' ");

        iApiDAO.updateSQL(sql.toString());
    }

    /**
     * 保存排行榜条件设置
     * @param api
     */
    public void rankSave(Api api) {
        iApiDAO.updateRankSet(api);
    }


}
